/*
	This file is part of Lproxy.

    Lproxy is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lproxy is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Lproxy.  If not, see <http://www.gnu.org/licenses/>.
*/
package lproxy_client;

import java.io.*;
import java.util.*;

class Connection
{
	public Connection(){}
	public Map<String,String> ParseHeader(String source)
	{
		Map<String,String> result = new HashMap<String,String>();
		int pos;
		if((pos = source.indexOf(CRLF+CRLF)) != -1)
		{
			result.put("content",source.substring(pos+(CRLF+CRLF).length(),source.length()));
			String header = source.substring(0,pos);
			String[] headers = header.split(CRLF);
			String[] firstLine = headers[0].split(" ",3);
			result.put("method",firstLine[0].toUpperCase());
			if((pos = firstLine[1].toLowerCase().indexOf("http://")) != -1)
			{
				result.put("path","/" + firstLine[1].split("/",4)[3]);
			}
			else
			{
				result.put("path",firstLine[1]);
			}
			result.put("protocol",firstLine[2]);
			for(int i=1;i<headers.length;i++)
			{
				if((headers[i] != null) && (headers[i].equals("") == false) && ((pos = headers[i].indexOf(":")) != -1))
				{
					result.put(headers[i].substring(0,pos).toLowerCase().trim(),headers[i].substring(pos + 1,headers[i].length()).trim());
				}
			}
			
			if(result.get("host").indexOf(":") != -1)
			{
				String[] host = result.get("host").split(":",2);
				result.put("address",host[0]);
				result.put("port",host[1]);
			}
			else
			{
				result.put("address",result.get("host"));
				result.put("port","80");
			}
		}
		result.put("connection","close");
		result.remove("keep-alive");
		result.remove("proxy-connection");
		return result;
	}

	protected final int TIME_OUT = 30;
	protected final int MAX_BUFFER_SIZE = 1024;
	protected final String CRLF = "\r\n";
	protected InputStream m_in;
	protected OutputStream m_out; 
}
